package com.hanyxx.thread;

import lombok.SneakyThrows;

/**
 * @author layman
 * @date 2021/2/5
 */
public class Demo05 {
    public static void main(String[] args) {
        Demo05Service service = new Demo05Service();
        Demo05ThreadA threadA = new Demo05ThreadA(service);
        threadA.start();
        Demo05ThreadB threadB = new Demo05ThreadB(service);
        threadB.start();
    }
}
class Demo05Service{
    int number;
    @SneakyThrows
    public void changeNum(String username){
        if("layman".equals(username)){
            number = 100;
            //线程睡眠，让后续线程可以修改number的值
            Thread.sleep(2000);
        }else{
            number = 200;
        }
        System.out.println("username: ["+username+"] number: ["+number+"]");
    }
}
class Demo05ThreadA extends  Thread{
    private Demo05Service service;
    public Demo05ThreadA(Demo05Service service){
        this.service = service;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            Thread.sleep(1000);
            service.changeNum("layman");
        }
    }
}
class Demo05ThreadB extends  Thread{
    private Demo05Service service;
    public Demo05ThreadB(Demo05Service service){
        this.service = service;
    }
    @SneakyThrows
    @Override
    public void run() {
        while (true) {
            Thread.sleep(1000);
            service.changeNum("菠菜饭团");
        }
    }
}
